package com.codinbros.session001.view;

import processing.core.PApplet;
import processing.core.PVector;

import com.codinbros.session001.interfaces.Citizen;
import com.codinbros.session001.model.ForceField;

public class Butterfly implements Citizen {
	private PVector position;
	private ForceField forceField;
	private PApplet g;
	private boolean paused = false;
	private PVector velocity;
	private PVector accelecation;
	private float maxSpeed = 2.0f;
	private float maxForce = 0.05f;
	private int width = 20;
	private int height = 20;

	public Butterfly() {

	}

	public Butterfly(PApplet pa) {
		g = pa;
		position = new PVector(0, 0);
	}

	public Butterfly(PApplet pa, PVector position) {
		g = pa;
		this.position = position;
		forceField = new ForceField();
	}

	public Butterfly(PApplet pa, PVector position, ForceField forceField) {
		g = pa;
		this.position = position;
		this.forceField = forceField;
	}

	public void draw() {
		if (!paused) {
			position.add(velocity);
			warp();
			PVector pointing;
			if (velocity.mag() > 0)
			{
				pointing = new PVector();
				pointing.add(velocity);
			}
			else
			{
				pointing = new PVector(1,1);
			}
			pointing.normalize();
			pointing.mult(height);
			
			float x = position.x;
			float y = position.y;
			
			g.stroke(255, 0, 0);
			g.strokeWeight(2);
			g.line(x, y, x + pointing.x, y + pointing.y);
			
			PVector topPointing = new PVector();
			topPointing.add(pointing);
			topPointing.mult(0.8f);
			
			PVector halfPointing = new PVector();
			halfPointing.add(pointing);
			halfPointing.mult(0.5f);
			
			PVector halfRightPointing = new PVector();
			halfRightPointing.x = halfPointing.y;
			halfRightPointing.y = -halfPointing.x;
			
			PVector halfLeftPointing = new PVector();
			halfLeftPointing.x = -halfRightPointing.x;
			halfLeftPointing.y = -halfRightPointing.y;
			
			PVector bottomPointing = new PVector();
			bottomPointing.add(pointing);
			bottomPointing.mult(0.1f);

			g.strokeWeight(1);
			g.line(x + halfPointing.x, y + halfPointing.y, x + halfPointing.x + halfRightPointing.x, y + halfPointing.y + halfRightPointing.y);
			g.line(x + halfPointing.x, y + halfPointing.y, x + halfPointing.x + halfLeftPointing.x, y + halfPointing.y + halfLeftPointing.y);
			
			//Right wing
			g.bezier(x + topPointing.x, y + topPointing.y, 
					x + topPointing.x + halfPointing.x + halfRightPointing.x, y + topPointing.y + halfPointing.y + halfRightPointing.y, 
					x + halfPointing.x + halfRightPointing.x * 2, y + halfPointing.y + halfRightPointing.y * 2, 
					x + halfPointing.x + halfRightPointing.x, y + halfPointing.y + halfRightPointing.y);
			g.bezier(x + halfPointing.x + halfRightPointing.x * 0.5f, y + halfPointing.y + halfRightPointing.y * 0.5f, 
					x + halfPointing.x + halfRightPointing.x * 2f, y + halfPointing.y + halfRightPointing.y * 2f, 
					x - halfPointing.x, y - halfPointing.y,
					x + bottomPointing.x, y + bottomPointing.y);
			
			//Left wing
			g.bezier(x + topPointing.x, y + topPointing.y, 
					x + topPointing.x + halfPointing.x + halfLeftPointing.x, y + topPointing.y + halfPointing.y + halfLeftPointing.y, 
					x + halfPointing.x + halfLeftPointing.x * 2, y + halfPointing.y + halfLeftPointing.y * 2, 
					x + halfPointing.x + halfLeftPointing.x, y + halfPointing.y + halfLeftPointing.y);
			g.bezier(x + halfPointing.x + halfLeftPointing.x * 0.5f, y + halfPointing.y + halfLeftPointing.y * 0.5f, 
					x + halfPointing.x + halfLeftPointing.x * 2f, y + halfPointing.y + halfLeftPointing.y * 2f, 
					x - halfPointing.x, y - halfPointing.y,
					x + bottomPointing.x, y + bottomPointing.y);
			
		}
	}

	private void warp() {
	    if (this.position.x < -width) this.position.x = g.width + this.width;
	    if (this.position.y < -height) this.position.y = g.height + this.height;
	    if (this.position.x > g.width+this.width) this.position.x = -width;
	    if (this.position.y > g.height+this.height) this.position.y = -height;
	}
	@Override
	public void affect(PVector force) {
		// TODO Auto-generated method stub
	}

	@Override
	public ForceField getEffect() {
		return forceField;
	}

	@Override
	public PVector getPostition() {
		return position;
	}

	@Override
	public void setPosition(PVector position) {
		this.position = position;
	}

	@Override
	public void terminate() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		paused = true;

	}

	@Override
	public void resume() {
		paused = false;

	}

	@Override
	public void setPapplet(PApplet pa) {
		g = pa;
	}

	@Override
	public PVector getVelocity() {
		return velocity;
	}

	@Override
	public void setVelocity(PVector velocity) {
		this.velocity = velocity;
	}

	@Override
	public void setMaxSpeed(float maxSpeed) {
		this.maxSpeed = maxSpeed;

	}

	@Override
	public float getMaxSpeed() {
		return maxSpeed;
	}

	@Override
	public void setMaxForce(float maxForce) {
		this.maxForce = maxForce;

	}

	@Override
	public float getMaxForce() {
		return maxForce;
	}

	@Override
	public PVector getAccellectaion() {
		return accelecation;
	}

	@Override
	public void setAccelleration(PVector acelleration) {
		this.accelecation = acelleration;
	}

}
